package com.tdogg.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.tdogg.entity.Users;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.pool.DruidDataSource;
import com.tdogg.entity.Admins;
import com.tdogg.entity.Funs;
import com.tdogg.service.IAdminService;
import com.tdogg.util.MD5Utils;

@Controller
@Scope("prototype")
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(String adminName, String adminPass, HttpServletRequest request) {
        Admins admin = adminService.login(adminName, adminPass);
        if (admin != null) {
            List<Funs> list = admin.getAdminRole().getRoleFuns();
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);
            return "success";
        }
        return "fail";
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("admin");
        return "forward:/view/admin";
    }
    /**
     * 	更改用户密码
     * @param username
     * @param cellphone
     * @param oldPass
     * @param newPass
     * @param request
     * @return
     */
    @RequestMapping("updatePass")
    @ResponseBody
    public String updatePass(String username, String cellphone, String oldPass, String newPass, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Admins admin = (Admins) session.getAttribute("admin");
        if (MD5Utils.passToMD5(oldPass+admin.getAdminSalt()).equals(admin.getAdminPass())) {
        	admin.setAdminSalt(RandomStringUtils.randomAscii(12));
            admin.setAdminPass(MD5Utils.passToMD5(newPass+admin.getAdminSalt()));
            admin.setAdminName(username);
            admin.setAdminPhone(cellphone);
            adminService.updateAdmin(admin);
            session.removeAttribute("admin");
            return "success";
        }
        return "fail";
    }

    @RequestMapping("findAllByPage")
    @ResponseBody
    public JSONObject findAllAdmin(Integer page, Integer limit, String keyword) throws UnsupportedEncodingException {
        if(StringUtils.isNoneBlank(keyword)) {
            keyword = URLDecoder.decode(URLEncoder.encode(keyword,"iso8859-1"),"utf-8");
        }
        PageInfo<Admins> info = adminService.queryByPage(page, limit, keyword);
        JSONObject obj = new JSONObject();
        obj.put("msg", "");
        obj.put("code", 0);
        obj.put("count", info.getTotal());
        obj.put("data", info.getList());
        return obj;
    }

    @RequestMapping("updateAdminState")
    @ResponseBody
    public String changeBannerState(Integer state,Integer adminId){
        Integer rs = adminService.updateAdminState(adminId,state);
        if(rs>0){
            return "success";
        }else{
            return "fail";
        }
    }


    @RequestMapping("updateAdminInfo")
    @ResponseBody
    public String updateAdminInfo(Admins admin){
        Integer rs = adminService.updateAdmin(admin);
        if(rs>0){
            return "success";
        }else{
            return "fail";
        }
    }

    @RequestMapping("adAdmin")
    @ResponseBody
    public String addAdmin(Admins admin){
        String s = adminService.addAdmin(admin);
        return s;
    }

    @RequestMapping("deleteAdmin")
    @ResponseBody
    public String deleteAdmin(Integer adminId){
        Integer rs = adminService.deleteAdmin(adminId);
        if(rs>0){
            return "success";
        }else{
            return "fail";
        }
    }
}
